package com.ryskoo.controller.customer;

import com.ryskoo.customer.vendor.domain.VendorPO;
import com.ryskoo.customer.vendor.service.IVendorService;
import com.ryskoo.customer.vendorinfo.domain.VendorInfoPO;
import com.ryskoo.customer.vendorinfo.service.IVendorInfoService;
import com.ryskoo.framework.core.bean.ReturnCodeEnum;
import com.ryskoo.framework.core.bean.ReturnMessageDTO;
import com.ryskoo.framework.core.controller.BasicHandler;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.utils.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/vendor/vendorinfo")
public class VendorInfoController extends BasicHandler<VendorInfoPO> {
    private static final String TEMPLATE_PATH = "system/customer/vendorinfo";

    private static final String MODULE_NAME = "VendorInfo";
    /**
     * 基类构造方法.
     *
     */
    public VendorInfoController() {
        super(TEMPLATE_PATH, MODULE_NAME);
    }

    private IVendorInfoService vendorInfoService;

    /**
     * 消费者注入
     *
     * @param vendorInfoService 消费者基类
     */
    @Autowired
    protected void setService(IVendorInfoService vendorInfoService) {
        this.vendorInfoService = vendorInfoService;
        super.setService(vendorInfoService);
    }

    @Autowired
    private IVendorService vendorService;

    /**
     * 积分换算设置
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/tosetintegral")
    public String toSetIntegral(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorInfoPO vendorInfo = vendorInfoService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorInfo", vendorInfo);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetIntegral";
    }

    @PostMapping("/setIntegral")
    @ResponseBody
    public ReturnMessageDTO setIntegral(@RequestBody VendorInfoPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorInfoService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }

    /**
     * 评价送积分设置
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/toappintegral")
    public String toAppIntegral(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorInfoPO vendorInfo = vendorInfoService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorInfo", vendorInfo);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetAppIntegral";
    }

    @PostMapping("/setAppIntegral")
    @ResponseBody
    public ReturnMessageDTO setAppIntegral(@RequestBody VendorInfoPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorInfoService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }

    /**
     * 营业状态设置
     * @param model
     * @param request
     * @return
     */
    @GetMapping("/toyystate")
    public String toYyState(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorInfoPO vendorInfo = vendorInfoService.getByVendorUuid(vendorUuid);
        model.addAttribute("vendorInfo", vendorInfo);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "SetYyState";
    }

    @PostMapping("/setYyState")
    @ResponseBody
    public ReturnMessageDTO setYyState(@RequestBody VendorInfoPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorInfoService.update(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }

    /**
     *
     * 店铺设置
     *
     * @param model
     * @param request
     * @return
     * @auther linq
     * @date 2018/11/8 14:43
     */
    @GetMapping("/toUpdate")
    public String toUpdate(Model model, HttpServletRequest request) {
        String vendorUuid = LoginUtil.getLoginVendorUuid();
        VendorPO vendor = vendorService.getByUuid(vendorUuid);
        model.addAttribute("vendor", vendor);
        return TEMPLATE_PATH + "/" + MODULE_NAME + "Update";
    }

    /**
     *
     * 店铺Logo及店铺信息修改
     *
     * @param po
     * @return
     * @auther linq
     * @date 2018/11/8 14:43
     */
    @PostMapping("/update")
    @ResponseBody
    public ReturnMessageDTO update(@RequestBody VendorPO po) {
        ReturnMessageDTO returnMessage = new ReturnMessageDTO();
        try {
            vendorInfoService.updateVendorInfo(po);
            returnMessage.setReturnCode(ReturnCodeEnum.SUCCESS.getReturnCode());
            returnMessage.setReturnNote("设置成功");
        } catch (InvalidParamException e) {
            returnMessage.setReturnCode(ReturnCodeEnum.FAIL.getReturnCode());
            returnMessage.setReturnNote(e.getMessage());
        } catch (Exception e) {
            returnMessage.setReturnCode(ReturnCodeEnum.ERROR.getReturnCode());
            returnMessage.setReturnNote("设置失败");
        }
        return returnMessage;
    }
}
